package org.dairy.farms.nandana.core;

import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jul 6, 2011
 * Time: 1:47:41 PM
 */
@Service(value = "nandanaUtils")
public class NandanaUtils {
    public Date mergeDateAndTime(String date,String time){
        String timer = time.split("T")[1];
        String dater = date.split("T")[0];
        date = dater+"T"+timer;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            return formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //Expeecting format to recevie 2011-07-28T00:00:00
    public Date getDate(String date,String format){
        if(format==null||format.equals("")){
            format = "yyyy-MM-dd'T'HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            return formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //Expeecting format to recevie 2011-07-28T00:00:00
    public Date getDate(String date){
        String format = "yyyy-MM-dd'T'HH:mm:ss";
        if(date==null){
            return null;
        }
       return getDate(date,format);
    }
    public static void main(String s[]){
        //Date date = new NandanaUtils().mergeDateAndTime("2012-05-11T00:00:00","2008-01-01T00:30:00");
        Date date = new NandanaUtils().getDate("10/20/2011","M/d/yyyy");
        System.out.println("Date received is "+date);
    }
}
